﻿using ConfExpert.Forms.Base;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraPivotGrid;
using DevTeam;

using System;
using System.Windows.Forms;

namespace ConfExpert.Forms.Depo
{
    public partial class Hareket : XForm
    {
        public Hareket()
        {
            InitializeComponent();
        }

        private void Hareket_Load(object sender, EventArgs e)
        {
            

            deTarih1.EditValue = DateTime.Now.AddMonths(-1);
            deTarih2.EditValue = DateTime.Now;
            cmbTarih.SelectedIndexChanged += new EventHandler(cmbTarih_SelectedIndexChanged);
            cmbTarih.SelectedIndex = 2;

            ExpandCollapse(splitContainerControl1, AcKapa.Collapse);

            //DirectionChanged();

            cmbGC.SelectedIndex = 2;
            cmbGC.SelectedIndexChanged += new EventHandler(cmbGC_SelectedIndexChanged);

            bs = gv.Init("GetHareket_OA", (cmbGC.SelectedIndex == 0 ? 4 : (cmbGC.SelectedIndex == 1 ? 10 : (cmbGC.SelectedIndex == 2 ? 100 : 0))), cmbTip.SelectedIndex, intParse(gle.EditValue), deTarih1.DateTime, deTarih2.DateTime, cmbTarih.SelectedIndex);
            DrillDownGrid(gridView2, bs);

            //bs = new BindingSource(ds, "xHareket");
            pivotGridControl1.DataSource = bs;

            //gridControl2.DataSource = null;
            //InitGridView(gridView2, bs);
            gridView2.AddSummary("QtyGelen", "QtyCikan");


            gridView2.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None;

            pivotGridControl1.FieldValueDisplayText += new PivotFieldDisplayTextEventHandler(pivotGrid_FieldValueDisplayText);
            pivotGridControl1.CellClick += new PivotCellEventHandler(pivotGridControl1_CellClick);
            pivotGridControl1.OptionsBehavior.ApplyBestFitOnFieldDragging = true;

            cmbKirilim.Properties.Items.AddRange(new object[] { "Tarih", "HareketTipi", "CikisEmriId", "GelisPartisiId", "SiparisNo", "BrandId" });
            cmbKirilim.SelectedIndexChanged += new EventHandler(cmbKirilim_SelectedIndexChanged);
            cmbKirilim.SelectedIndex = 0;

            btnGetir.Click += new EventHandler(btnGetir_Click);
            cmbGC.Properties.TextEditStyle = 
            cmbTip.Properties.TextEditStyle = TextEditStyles.DisableTextEditor;
            cmbTip.SelectedIndexChanged += new EventHandler(cmbTip_SelectedIndexChanged);

            gle.EditValueChanged += new EventHandler(gle_EditValueChanged);
            //DrillDownGrid(gridView2);
            gle.BorderStyle = DevExpress.XtraEditors.Controls.BorderStyles.Default;
            gle.Visible = false;
        }

        bool suspended = false;
        void gle_EditValueChanged(object sender, EventArgs e)
        {
            if (!suspended)
                btnGetir.PerformClick();
        }

        void cmbTip_SelectedIndexChanged(object sender, EventArgs e)
        {
            suspended = true;
            gle.EditValue = null;
            gle.BorderStyle = DevExpress.XtraEditors.Controls.BorderStyles.Default;
            gle.Visible = (cmbTip.SelectedIndex > -1);
            suspended = false;

            switch (cmbTip.SelectedIndex)
            {
                case 0: 
                    gle.Properties.Assign(RI(Rp.Atelye)); 
                    break;
                case 1: 
                    gle.Properties.Assign(RI(Rp.Vendor)); 
                    break;
                case 2: 
                    gle.Properties.Assign(RI(Rp.V_Customer)); 
                    break;
            }
        }

        void cmbGC_SelectedIndexChanged(object sender, EventArgs e)
        {
            //DirectionChanged();

            pivotGridControl1.Fields["QtyGelen"].Visible = cmbGC.SelectedIndex == 0 || cmbGC.SelectedIndex == 2;
            pivotGridControl1.Fields["QtyCikan"].Visible = cmbGC.SelectedIndex == 1 || cmbGC.SelectedIndex == 2;
            btnGetir.PerformClick();
        }

        private void DirectionChanged()
        {
            //bs = new BindingSource(ds, (cmbGC.SelectedIndex == 0 ? "xHareketG" : "xHareketC"));
        }

        void glestCikis_EditValueChanged(object sender, EventArgs e)
        {
            btnGetir.PerformClick();
        }

        void btnGetir_Click(object sender, EventArgs e)
        {
            gv.RefreshDataSource(true, "GetHareket_OA", (cmbGC.SelectedIndex == 0 ? 4 : (cmbGC.SelectedIndex == 1 ? 10 : (cmbGC.SelectedIndex == 2 ? 100 : 0))), cmbTip.SelectedIndex, intParse(gle.EditValue), deTarih1.DateTime, deTarih2.DateTime, cmbTarih.SelectedIndex);
            pivotGridControl1.Update();
            //pivotGridControl1.BestFit();
        }

        void cmbTarih_SelectedIndexChanged(object sender, EventArgs e)
        {
            //küçük, büyük, arasında
            switch (cmbTarih.SelectedIndex)
            {
                case 0: 
                    deTarih1.Enabled = false; deTarih2.Enabled = true; 
                    break;
                case 1: 
                    deTarih1.Enabled = true; deTarih2.Enabled = false; 
                    break;
                default: 
                    deTarih1.Enabled = deTarih2.Enabled = true; 
                    break;
            }
        }

        PivotGridField[] fields2;
        PivotGridField fieldProduct;
        bool isInitialized = false;
        void ArrangePg()
        {
            gridControl2.DataSource = null;
            PivotGridField[] fields;
            PivotGridField[] filters;

            pivotGridControl1.BeginUpdate();
            if (!isInitialized)
            {
                filters = new PivotGridField[7];
                filters[0] = AddNewField(pivotGridControl1, "BrandId", PivotArea.FilterArea);
                filters[0].FieldEdit = RI(Rp.Brand);

                filters[1] = AddNewField(pivotGridControl1, "SezonId", PivotArea.FilterArea);
                filters[1].FieldEdit = RI(Rp.Sezon);

                filters[2] = AddNewField(pivotGridControl1, "OrderRef", PivotArea.FilterArea);

                filters[3] = AddNewField(pivotGridControl1, "HareketTipiId", PivotArea.FilterArea);
                filters[3].FieldEdit = RI(Rp.HareketTipi);

                filters[4] = AddNewField(pivotGridControl1, "AtelyeId", PivotArea.FilterArea);
                filters[4].FieldEdit = RI(Rp.Atelye);

                filters[5] = AddNewField(pivotGridControl1, "VendorId", PivotArea.FilterArea);
                filters[5].FieldEdit = RI(Rp.Vendor);

                filters[6] = AddNewField(pivotGridControl1, "CustomerId", PivotArea.FilterArea);
                //filters[6].FieldEdit = RI(Rp.V_Customer);

                
                fields = new PivotGridField[1];
                //fields[0] = AddNewField("AtelyeId", PivotArea.RowArea);
                //fields[0].FieldEdit = RI(Rp.Atelye);

                fields[0] = AddNewField(pivotGridControl1, "ProductId", PivotArea.RowArea);
                fields[0].FieldEdit = RI(Rp.V_Kumas);
                pivotGridControl1.Groups.Add(fields, "AtelyeProduct");
                fieldProduct = fields[0];  //sonradan erişebilmek için

                AddNewField(pivotGridControl1, "QtyGelen", PivotArea.DataArea);
                pivotGridControl1.Fields["QtyGelen"].CellFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
                pivotGridControl1.Fields["QtyGelen"].CellFormat.FormatString = "#,###.##";

                AddNewField(pivotGridControl1, "QtyCikan", PivotArea.DataArea);
                pivotGridControl1.Fields["QtyCikan"].CellFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
                pivotGridControl1.Fields["QtyCikan"].CellFormat.FormatString = "#,###.##";

                isInitialized = true;
            }
            else
            {
                for (int i = 0; i < fields2.Length; i++)
                    pivotGridControl1.Fields.Remove(fields2[i]);

                for (int i = 1; i < pivotGridControl1.Groups.Count; i++)
                    pivotGridControl1.Groups[i].Clear();  //0 indisi stok için!

            }

            switch (selKirilimIndex)
            {
                case 0:
                    fields2 = new PivotGridField[2];
                    //pivotGridControl1.Fields["Tarih"].ValueFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
                    //pivotGridControl1.Fields["Tarih"].ValueFormat.FormatString = "d";

                    PivotGridField fieldYear = new PivotGridField("Tarih", PivotArea.ColumnArea);
                    PivotGridField fieldMonth = new PivotGridField("Tarih", PivotArea.ColumnArea);

                    // Add the fields to the field collection.
                    pivotGridControl1.Fields.Add(fieldYear);
                    fieldYear.GroupInterval = PivotGroupInterval.DateYear;
                    fieldYear.Caption = "Year";

                    pivotGridControl1.Fields.Add(fieldMonth);
                    fieldMonth.GroupInterval = PivotGroupInterval.DateMonth;
                    fieldMonth.Caption = "Month";

                    fields2[0] = fieldYear;
                    fields2[1] = fieldMonth;
                    break;

                case 1:
                    fields2 = new PivotGridField[1];
                    fields2[0] = AddNewField(pivotGridControl1, "HareketTipiId", PivotArea.ColumnArea);
                    fields2[0].FieldEdit = RI(Rp.HareketTipi);
                    break;

                case 2:
                    fields2 = new PivotGridField[1];
                    fields2[0] = AddNewField(pivotGridControl1, "GelisPartisiId", PivotArea.ColumnArea);
                    break;

                case 3:
                    fields2 = new PivotGridField[1];
                    fields2[0] = AddNewField(pivotGridControl1, "CikisEmriId", PivotArea.ColumnArea);
                    //fields2[0].FieldEdit = RI(Rp.Depo);

                    //fields2[1] = AddNewField("ShelfId", PivotArea.ColumnArea);
                    //fields2[1].FieldEdit = RI(Rp.Shelf);
                    //pivotGridControl1.Groups.Add(fields2, "FDepoShelf");
                    break;

                case 4:
                    fields2 = new PivotGridField[1];
                    fields2[0] = AddNewField(pivotGridControl1, "SiparisNo", PivotArea.ColumnArea);
                    //pivotGridControl1.Groups.Add(fields2, "Parti");
                    break;

                case 5:
                    fields2 = new PivotGridField[1];
                    fields2[0] = AddNewField(pivotGridControl1, "BrandId", PivotArea.ColumnArea);
                    fields2[0].FieldEdit = RI(Rp.Brand);
                    //pivotGridControl1.Groups.Add(fields2, "Marka");
                    break;

            }

            foreach (PivotGridField pgf in pivotGridControl1.Fields)
            {
                pgf.SortOrder = PivotSortOrder.Ascending;
                pgf.Options.AllowFilter = DevExpress.Utils.DefaultBoolean.True;
                pgf.Options.AllowSort = DevExpress.Utils.DefaultBoolean.True;
            }
            pivotGridControl1.EndUpdate();
            //pivot.BestFit();
        }


        private void pivotGridControl1_CellClick(object sender, DevExpress.XtraPivotGrid.PivotCellEventArgs e)
        {
            //kırmızı çarpı veriyor bazen. engelle!
            gridControl2.DataSource = e.CreateDrillDownDataSource();
            if (gridControl2.DataSource == null)
                return;
            ExpandCollapse(splitContainerControl1, (gridView2.RowCount > 0 ? AcKapa.Expand : AcKapa.Collapse));
            gridView2.Columns["ProductId"].Visible = fieldProduct.Area != PivotArea.RowArea;
            gridView2.BestFitColumns();
            gridView2.Columns["QtyGelen"].Width = gridView2.Columns["QtyCikan"].Width = 100;

            //DEVEX'e sor
            //TODO: caption'a tıklayınca Goster(ProductId);
        }

        int selKirilimIndex = -1;
        private void cmbKirilim_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (selKirilimIndex != cmbKirilim.SelectedIndex)
            {
                selKirilimIndex = cmbKirilim.SelectedIndex;
                ArrangePg();
            }
        }
        BindingSource bs;

    }
}